﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.VisualStudio.Shell;
using EnvDTE;
namespace Aoxuelf.MDSTool
{
    public class FileCodeModelHelper
    {
        public static readonly string Namespace_DataAccess = "LY.MDS.DataAccess";
        public static readonly string Namespace_Service = "LY.MDS.Business";

        public static bool IsCSharpFile(DTE Dte)
        {
            return Dte.ActiveDocument != null && Dte.Solution.IsOpen &&
                Dte.ActiveDocument.ProjectItem != null &&
                Dte.ActiveDocument.ProjectItem.FileCodeModel != null &&
                Dte.ActiveDocument.ProjectItem.FileCodeModel.Language == CodeModelLanguageConstants.vsCMLanguageCSharp;
        }

        public static CodeClass GetCodeClass(DTE Dte)
        { 
            TextSelection textSelection = Dte.ActiveDocument.Selection as TextSelection;
            CodeClass codeClass = Dte.ActiveDocument.ProjectItem.FileCodeModel.CodeElementFromPoint(textSelection.ActivePoint, vsCMElement.vsCMElementClass) as CodeClass;
            return codeClass;
        }

        public static CodeFunction GetCodeFunction(DTE Dte)
        {
            TextSelection textSelection = Dte.ActiveDocument.Selection as TextSelection;
            CodeFunction codeFunc = Dte.ActiveDocument.ProjectItem.FileCodeModel.CodeElementFromPoint(textSelection.ActivePoint, vsCMElement.vsCMElementFunction) as CodeFunction;
            return codeFunc;
        }

        public static bool IsSpecialClass(DTE Dte, string nameSpace)
        {
            if (IsCSharpFile(Dte))
            {
                CodeClass klass = GetCodeClass(Dte);
                if (klass != null && klass.Namespace.FullName.StartsWith(nameSpace, StringComparison.CurrentCultureIgnoreCase))
                {
                    return true;
                }
            }

            return false;
        }

        public static bool IsDataAccessClass(DTE Dte)
        {
            return IsSpecialClass(Dte, FileCodeModelHelper.Namespace_DataAccess);
        }

        public static bool IsServceClass(DTE Dte)
        {
            return IsSpecialClass(Dte, FileCodeModelHelper.Namespace_Service);
        }
    }
}
